home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / doc.lha / doc.doc / reuse.doc < prev    next >
Text File  |  1992-09-25  |  53KB  |  2,066 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. ___________________________________________________________________
  12.  
  13.  
  14.                                    Reusable Software
  15.                                    A Collection of MODULA-Modules
  16.  
  17.                                    J. Grosch
  18.  
  19.  
  20. ___________________________________________________________________
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. ___________________________________________________________________
  51.                                    GESELLSCHAFT FUeR MATHEMATIK
  52.                                    UND DATENVERARBEITUNG MBH
  53.  
  54.                                    FORSCHUNGSSTELLE FUeR
  55.                                    PROGRAMMSTRUKTUREN
  56.                                    AN DER UNIVERSITAeT KARLSRUHE
  57. ___________________________________________________________________
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.                                    Project
  76.  
  77.                              Compiler Generation
  78.  
  79.          ___________________________________________________________
  80.  
  81.                               Reusable Software
  82.                         A Collection of MODULA-Modules
  83.  
  84.  
  85.                                  Josef Grosch
  86.  
  87.  
  88.                                  Aug. 4 1992
  89.  
  90.          ___________________________________________________________
  91.  
  92.  
  93.                                  Report No. 4
  94.  
  95.  
  96.                              Copyright c 1992 GMD
  97.  
  98.  
  99.             Gesellschaft fuer Mathematik und Datenverarbeitung mbH
  100.                 Forschungsstelle an der Universitaet Karlsruhe
  101.                           Vincenz-Priesznitz-Str. 1
  102.                                D-7500 Karlsruhe
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                                     Reuse                                    2
  135.  
  136.  
  137. Abstract
  138.  
  139. A brief description of my personal collection of reusable modules  written  in
  140. MODULA-2 is given.  Some modules have been translated to the language C.
  141.  
  142. 1.  Overview
  143.  
  144.      The following modules exist currently (Aug. 1992):
  145.  
  146.       __________________________________________________________________
  147.        Module      Task
  148.       __________________________________________________________________
  149.        Memory      dynamic storage (heap) with free lists
  150.        Heap        dynamic storage (heap) without free lists
  151.        DynArray    dynamic and flexible arrays
  152.        Strings     string handling
  153.        StringMem   string memory
  154.        Idents      identifier table - unambiguous encoding of strings
  155.        Lists       lists of arbitrary objects
  156.        Texts       texts are lists of strings (lines)
  157.        Sets        sets of scalar values (without run time checks)
  158.        SetsC       sets of scalar values (with run time checks)
  159.        Relations   binary relations between scalar values
  160.        IO          buffered input and output
  161.        StdIO       buffered IO for standard files
  162.        Layout      more routines for input and output
  163.        Positions   handling of source positions
  164.        Errors      error handler for parsers and compilers
  165.        Source      provides input for scanners
  166.        Sort        quicksort for arrays with elements of arbitrary type
  167.        General     miscellaneous functions
  168.        System      machine dependent code
  169.        Checks      checks for system calls
  170.        Times       access to cpu-time
  171.       __________________________________________________________________
  172.      
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                        
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221. 2.  Memory: dynamic storage (heap) with free lists
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.                                     Reuse                                    3
  248.  
  249.  
  250. DEFINITION MODULE Memory;
  251.  
  252. VAR       MemoryUsed    : LONGCARD;
  253.                         (* Holds the total amount of memory managed by  *)
  254.                         (* this module.                                 *)
  255.  
  256. PROCEDURE Alloc         (ByteCount: LONGINT) : ADDRESS;
  257.                         (* Returns a pointer to dynamically allocated   *)
  258.                         (* memory space of size 'ByteCount' bytes.      *)
  259.                         (* Returns NIL if space is exhausted.           *)
  260.  
  261. PROCEDURE Free          (ByteCount: LONGINT; a: ADDRESS);
  262.                         (* The dynamically allocated space starting at  *)
  263.                         (* address 'a' of size 'ByteCount' bytes is     *)
  264.                         (* released.                                    *)
  265.  
  266. END Memory.
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.                                     Reuse                                    4
  313.  
  314.  
  315. 3.  Heap: dynamic storage (heap) without free lists
  316.  
  317.  
  318. DEFINITION MODULE Heap;
  319.  
  320. VAR       HeapUsed      : LONGCARD;
  321.                         (* Holds the total amount of memory managed by  *)
  322.                         (* this module.                                 *)
  323.  
  324. PROCEDURE Alloc         (ByteCount: LONGINT) : ADDRESS;
  325.                         (* Returns a pointer to dynamically allocated   *)
  326.                         (* space of size 'ByteCount' bytes.             *)
  327.  
  328. PROCEDURE Free          ;
  329.                         (* The complete space allocated for the heap    *)
  330.                         (* is released.                                 *)
  331.  
  332. END Heap.
  333.  
  334.  
  335. 4.  DynArray: dynamic and flexible arrays
  336.  
  337.      This module provides dynamic and flexible arrays. The size of  a  dynamic
  338. array  is  determined  at run time. It must be passed to a procedure to create
  339. the array. The size of such an array is also flexible, that means it can  grow
  340. to arbitrary size by repeatedly calling a procedure to extend it.
  341.  
  342.  
  343. DEFINITION MODULE DynArray;
  344.  
  345. PROCEDURE MakeArray     (VAR ArrayPtr   : ADDRESS       ;
  346.                          VAR ElmtCount  : LONGINT       ;
  347.                              ElmtSize   : LONGINT)      ;
  348.                         (* 'ArrayPtr' is set to the start address of a  *)
  349.                         (* memory space to hold an array of 'ElmtCount' *)
  350.                         (* elements each of size 'ElmtSize' bytes.      *)
  351.  
  352. PROCEDURE ExtendArray   (VAR ArrayPtr   : ADDRESS       ;
  353.                          VAR ElmtCount  : LONGINT       ;
  354.                              ElmtSize   : LONGINT)      ;
  355.                         (* The memory space for the array is increased  *)
  356.                         (* by doubling the number of elements.          *)
  357.  
  358. PROCEDURE ReleaseArray  (VAR ArrayPtr   : ADDRESS       ;
  359.                          VAR ElmtCount  : LONGINT       ;
  360.                              ElmtSize   : LONGINT)      ;
  361.                         (* The memory space for the array is released.  *)
  362.  
  363. END DynArray.
  364.  
  365.  
  366. Example:
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.                                     Reuse                                    5
  378.  
  379.  
  380. There is a problem with the index arithmetic generated by  the  compiler.   If
  381. TSIZE  (ArrayType)  <  65536  then  index  arithmetic will be done with 2 byte
  382. values otherwise with 4 byte values.  Arithmetic  with  2  bytes  allows  only
  383. access  to  arrays whose size never exceeds 65536 bytes.  If larger arrays are
  384. desired then ArrayType has to be declared to yield a size greater or equal  to
  385. 65536 bytes like below.
  386.  
  387.  
  388. CONST InitialSize       = 100;
  389. TYPE  ElmtType          = ... ;
  390. TYPE  ArrayType         = ARRAY [0 .. 100000] OF ElmtType;
  391. VAR   ActualSize        : LONGINT;
  392. VAR   ArrayPtr          : POINTER TO ArrayType;
  393.  
  394. BEGIN
  395.    ActualSize := InitialSize;
  396.    MakeArray (ArrayPtr, ActualSize, TSIZE (ElmtType));
  397.  
  398.    (* Case 1: continuously growing array *)
  399.  
  400.    Index := 0;
  401.    LOOP
  402.       INC (Index);
  403.       IF Index = ActualSize THEN
  404.          ExtendArray (ArrayPtr, ActualSize, TSIZE (ElmtType));
  405.       END;
  406.  
  407.       ArrayPtr ^ [Index] := ... ;   (* access an array element *)
  408.        ... := ArrayPtr ^ [Index];   (* "      "  "     "       *)
  409.    END;
  410.  
  411.    (* Case 2: non-continuously growing array *)
  412.  
  413.    LOOP
  414.       Index := ... ;
  415.       WHILE Index >= ActualSize DO
  416.          ExtendArray (ArrayPtr, ActualSize, TSIZE (ElmtType));
  417.       END;
  418.  
  419.       ArrayPtr ^ [Index] := ... ;   (* access an array element *)
  420.        ... := ArrayPtr ^ [Index];   (* "      "  "     "       *)
  421.    END;
  422.  
  423.    ReleaseArray (ArrayPtr, ActualSize, TSIZE (ElmtType));
  424. END;
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.                                     Reuse                                    6
  443.  
  444.  
  445. 5.  Strings: string handling
  446.  
  447.      This module provides operations on strings whose length is  at  most  255
  448. characters.
  449.  
  450.  
  451. DEFINITION MODULE Strings;
  452.  
  453. CONST   cMaxStrLength   = 255;
  454.  
  455. TYPE    tString         ;
  456. TYPE    tStringIndex    = [0 .. cMaxStrLength];
  457.  
  458. PROCEDURE Assign        (VAR s1, s2: tString);
  459.                         (* Assigns the string 's2' to the string 's1'.  *)
  460.  
  461. PROCEDURE AssignEmpty   (VAR s: tString);
  462.                         (* Returns an empty string 's'.                 *)
  463.  
  464. PROCEDURE Concatenate   (VAR s1, s2: tString);
  465.                         (* Returns in parameter 's1' the concatenation  *)
  466.                         (* of the strings 's1' and 's2'.                *)
  467.  
  468. PROCEDURE Append        (VAR s: tString; c: CHAR);
  469.                         (* The character 'c' is concatenated at the end *)
  470.                         (* of the string 's'.                           *)
  471.  
  472. PROCEDURE Length        (VAR s: tString)                        : CARDINAL;
  473.                         (* Returns the length of the string 's'.        *)
  474.  
  475. PROCEDURE IsEqual       (VAR s1, s2: tString)                   : BOOLEAN;
  476.                         (* Returns TRUE if the strings 's1' and 's2'    *)
  477.                         (* are equal.                                   *)
  478.  
  479. PROCEDURE IsInOrder     (VAR s1, s2: tString)                   : BOOLEAN;
  480.                         (* Returns TRUE if the string 's1' is lexico-   *)
  481.                         (* graphically less or equal to the string 's2'.*)
  482.  
  483. PROCEDURE Exchange      (VAR s1, s2: tString);
  484.                         (* Exchanges the strings 's1' and 's2'.         *)
  485.  
  486. PROCEDURE SubString     (VAR s1: tString; from, to: tStringIndex; VAR s2: tString);
  487.                         (* Returns in 's2' the substring from 's1' com- *)
  488.                         (* prising the characters between 'from' and 'to'. *)
  489.                         (* PRE  1 <= from <= Length (s1)                *)
  490.                         (* PRE  1 <=  to  <= Length (s1)                *)
  491.  
  492. PROCEDURE Char          (VAR s: tString; i: tStringIndex)       : CHAR;
  493.                         (* Returns the 'i'-th character of the string 's'. *)
  494.                         (* The characters are counted from 1 to Length (s). *)
  495.                         (* PRE  1 <= index <= Length (s)                *)
  496.  
  497. PROCEDURE ArrayToString (a: ARRAY OF CHAR; VAR s: tString);
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.                                     Reuse                                    7
  509.  
  510.  
  511.                         (* An array 'a' of characters representing a    *)
  512.                         (* MODULA string is converted to a string 's'   *)
  513.                         (* of type tString.                             *)
  514.  
  515. PROCEDURE StringToArray (VAR s: tString; VAR a: ARRAY OF CHAR);
  516.                         (* A string 's' of type tString is converted to *)
  517.                         (* an array 'a' of characters representing a    *)
  518.                         (* MODULA string.                               *)
  519.  
  520. PROCEDURE StringToInt   (VAR s: tString)                        : INTEGER;
  521.                         (* Returns the integer value represented by 's'. *)
  522.  
  523. PROCEDURE StringToNumber(VAR s: tString; Base: CARDINAL)        : CARDINAL;
  524.                         (* Returns the integer value represented by 's' *)
  525.                         (* to the base 'Base'.                          *)
  526.  
  527. PROCEDURE StringToReal  (VAR s: tString)                        : REAL;
  528.                         (* Returns the real value represented by 's'.   *)
  529.  
  530. PROCEDURE IntToString   (n: INTEGER; VAR s: tString);
  531.                         (* Returns in 's' the string representation of 'n'. *)
  532.  
  533. PROCEDURE ReadS         (f: tFile; VAR s: tString; FieldWidth: tStringIndex);
  534.                         (* Read 'FieldWidth' characters as string 's'   *)
  535.                         (* from file 'f'.                               *)
  536.  
  537. PROCEDURE ReadL         (f: tFile; VAR s: tString);
  538.                         (* Read rest of line as string 's' from file    *)
  539.                         (* 'f'. Skip to next line.                      *)
  540.  
  541. PROCEDURE WriteS        (f: tFile; VAR s: tString);
  542.                         (* Write string 's' to file 'f'.                *)
  543.  
  544. PROCEDURE WriteL        (f: tFile; VAR s: tString);
  545.                         (* Write string 's' as complete line.           *)
  546.  
  547. END Strings.
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.                                     Reuse                                    8
  574.  
  575.  
  576. 6.  StringMem: string memory
  577.  
  578.  
  579. DEFINITION MODULE StringMem;
  580.  
  581. TYPE tStringRef         ;
  582.  
  583. PROCEDURE PutString     (VAR s: tString)                        : tStringRef;
  584.                         (* Stores string 's' in the string memory and   *)
  585.                         (* returns a reference to the stored string.    *)
  586.  
  587. PROCEDURE GetString     (r: tStringRef;                    VAR s: tString);
  588.                         (* Returns the string 's' from the string       *)
  589.                         (* memory which is referenced by 'r'.           *)
  590.  
  591. PROCEDURE Length        (r: tStringRef)                         : CARDINAL;
  592.                         (* Returns the length of the string 's'         *)
  593.                         (* which is referenced by 'r'.                  *)
  594.  
  595. PROCEDURE IsEqual       (r: tStringRef; VAR s: tString)         : BOOLEAN;
  596.                         (* Compares the string referenced by 'r' and    *)
  597.                         (* the string 's'.                              *)
  598.                         (* Returns TRUE if both are equal.              *)
  599.  
  600. PROCEDURE WriteString   (f: tFile; r: tStringRef);
  601.                         (* The string referenced by 'r' is printed on   *)
  602.                         (* the file 'f'.                                *)
  603.  
  604. PROCEDURE WriteStringMemory;
  605.                         (* The contents of the string memory is printed *)
  606.                         (* on the file 'StdOutput'.                     *)
  607.  
  608. PROCEDURE InitStringMemory;
  609.                         (* The string memory is initialized.            *)
  610.  
  611. END StringMem.
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.                                     Reuse                                    9
  639.  
  640.  
  641. 7.  Idents: identifier table - unambiguous encoding of strings
  642.  
  643.  
  644. DEFINITION MODULE Idents;
  645.  
  646. TYPE      tIdent        ;
  647.  
  648. VAR       NoIdent       : tIdent;       (* a default (empty) identifier *)
  649.  
  650. PROCEDURE MakeIdent     (VAR s: tString)                : tIdent;
  651.                         (* The string 's' is mapped to a unique         *)
  652.                         (* identifier (an integer) which is returned.   *)
  653.  
  654. PROCEDURE GetString     (i: tIdent; VAR s: tString);
  655.                         (* Returns the string 's' whose identifier is 'i'. *)
  656.  
  657. PROCEDURE GetStringRef  (i: tIdent)                     : tStringRef;
  658.                         (* Returns a reference to a string whose        *)
  659.                         (* identifier is 'i'.                           *)
  660.  
  661. PROCEDURE MaxIdent      ()                              : tIdent;
  662.                         (* Returns the current maximal value of the     *)
  663.                         (* type 'tIdent'.                               *)
  664.  
  665. PROCEDURE WriteIdent    (f: tFile; i: tIdent);
  666.                         (* The string encoded by the identifier 'i' is  *)
  667.                         (* printed on the file 'f'.                     *)
  668.  
  669. PROCEDURE WriteIdents   ;
  670.                         (* The contents of the identifier table is      *)
  671.                         (* printed on the file 'StdOutput'.             *)
  672.  
  673. PROCEDURE InitIdents    ;
  674.                         (* The identifier table is initialized.         *)
  675.  
  676. END Idents.
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.                                     Reuse                                   10
  704.  
  705.  
  706. 8.  Lists: lists of arbitrary objects
  707.  
  708.      This module actually handles lists of untyped pointers.  Thus it is  pos-
  709. sible to have lists of pointers referring to objects of arbitrary types.
  710.  
  711.  
  712. DEFINITION MODULE Lists;
  713.  
  714. TYPE tList              ;
  715.  
  716. PROCEDURE MakeList      (VAR List: tList);
  717.                         (* Create an empty list.                        *)
  718.  
  719. PROCEDURE Insert        (VAR List: tList; Elmt: ADDRESS);
  720.                         (* Add element at the beginning of the list.    *)
  721.  
  722. PROCEDURE Append        (VAR List: tList; Elmt: ADDRESS);
  723.                         (* Add element at the end of the list.          *)
  724.  
  725. PROCEDURE Head          (    List: tList): ADDRESS;
  726.                         (* Return first element of the list.            *)
  727.  
  728. PROCEDURE Tail          (VAR List: tList);
  729.                         (* Return list except first element.            *)
  730.  
  731. PROCEDURE Last          (    List: tList): ADDRESS;
  732.                         (* Return last element of the list.             *)
  733.  
  734. PROCEDURE Front         (VAR List: tList);
  735.                         (* Return list except last element.             *)
  736.                         (* Not implemented.                             *)
  737.  
  738. PROCEDURE IsEmpty       (    List: tList): BOOLEAN;
  739.                         (* Returns TRUE if the list is empty.           *)
  740.  
  741. PROCEDURE Length        (    List: tList): CARDINAL;
  742.                         (* Returns the number of elements in the list.  *)
  743.  
  744. PROCEDURE WriteList     (f: tFile; List: tList; Proc: tProcOfFileAddress);
  745.                         (* Call Proc (f, e) for all elements of a list. *)
  746.                         (* e points to the current list element.        *)
  747.                         (* Can be used e. g. to print a list.           *)
  748.  
  749. END Lists.
  750.  
  751.  
  752.  
  753.  
  754.  
  755.  
  756.  
  757.  
  758.  
  759.  
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
  766.  
  767.  
  768.                                     Reuse                                   11
  769.  
  770.  
  771. 9.  Texts: texts are lists of strings (lines)
  772.  
  773.  
  774. DEFINITION MODULE Texts;
  775.  
  776. TYPE tText              ;
  777.  
  778. PROCEDURE MakeText      (VAR Text: tText);
  779.                         (* Create an empty text.                        *)
  780.  
  781. PROCEDURE Append        (VAR Text: tText; VAR String: tString);
  782.                         (* Add a line at the beginning of text 'Text'.  *)
  783.  
  784. PROCEDURE Insert        (VAR Text: tText; VAR String: tString);
  785.                         (* Add a line at the end of the text 'Text'.    *)
  786.  
  787. PROCEDURE IsEmpty       (VAR Text: tText): BOOLEAN;
  788.                         (* Test whether a text 'Text' is empty.         *)
  789.  
  790. PROCEDURE WriteText     (f: tFile; Text: tText);
  791.                         (* Print the text 'Text' on the file 'f'.       *)
  792.  
  793. END Texts.
  794.  
  795.  
  796. 10.  Sets: sets for scalar values
  797.  
  798.      The following module provides operations on sets of  scalar  values.  The
  799. elements  of  the  sets  can be of the types INTEGER, CARDINAL, CHAR, or of an
  800. enumeration type. Elements of type CHAR or of enumeration  types  have  to  be
  801. coerced  to  the type CARDINAL with the function ORD before use as a parameter
  802. of the functions below. The size of the sets, that is the range  the  elements
  803. must  lie  in,  is not restricted. The elements can range from 0 to 'MaxSize',
  804. where space for arbitrary large sets.
  805.  
  806.      The sets are implemented as bit vectors (ARRAY OF BITSET) plus some addi-
  807. tional information to improve performance. So don't worry about speed too much
  808. because procedures like Select, Extract, or Card  are  quite  efficient.  They
  809. don't  execute a loop over all potentially existing elements always. This hap-
  810. pens only in the worst case.
  811.  
  812.  
  813.  
  814.  
  815.  
  816.  
  817.  
  818.  
  819.  
  820.  
  821.  
  822.  
  823.  
  824.  
  825.  
  826.  
  827.  
  828.  
  829.  
  830.  
  831.  
  832.  
  833.                                     Reuse                                   12
  834.  
  835.  
  836. DEFINITION MODULE Sets;
  837.  
  838. TYPE tSet;
  839. TYPE ProcOfCard         = PROCEDURE (CARDINAL);
  840. TYPE ProcOfCardToBool   = PROCEDURE (CARDINAL): BOOLEAN;
  841.  
  842. PROCEDURE MakeSet       (VAR Set: tSet; MaxSize: CARDINAL);
  843. PROCEDURE ReleaseSet    (VAR Set: tSet);
  844. PROCEDURE Union         (VAR Set1: tSet; Set2: tSet);
  845. PROCEDURE Difference    (VAR Set1: tSet; Set2: tSet);
  846. PROCEDURE Intersection  (VAR Set1: tSet; Set2: tSet);
  847. PROCEDURE SymDiff       (VAR Set1: tSet; Set2: tSet);
  848. PROCEDURE Complement    (VAR Set: tSet);
  849. PROCEDURE Include       (VAR Set: tSet; Elmt: CARDINAL);
  850. PROCEDURE Exclude       (VAR Set: tSet; Elmt: CARDINAL);
  851. PROCEDURE Card          (VAR Set: tSet): CARDINAL;
  852. PROCEDURE Size          (VAR Set: tSet): CARDINAL;
  853. PROCEDURE Minimum       (VAR Set: tSet): CARDINAL;
  854. PROCEDURE Maximum       (VAR Set: tSet): CARDINAL;
  855. PROCEDURE Select        (VAR Set: tSet): CARDINAL;
  856. PROCEDURE Extract       (VAR Set: tSet): CARDINAL;
  857. PROCEDURE IsSubset      (Set1, Set2: tSet): BOOLEAN;
  858. PROCEDURE IsStrictSubset(Set1, Set2: tSet): BOOLEAN;
  859. PROCEDURE IsEqual       (VAR Set1, Set2: tSet): BOOLEAN;
  860. PROCEDURE IsNotEqual    (Set1, Set2: tSet): BOOLEAN;
  861. PROCEDURE IsElement     (Elmt: CARDINAL; VAR Set: tSet): BOOLEAN;
  862. PROCEDURE IsEmpty       (Set: tSet): BOOLEAN;
  863. PROCEDURE Forall        (Set: tSet; Proc: ProcOfCardToBool): BOOLEAN;
  864. PROCEDURE Exists        (Set: tSet; Proc: ProcOfCardToBool): BOOLEAN;
  865. PROCEDURE Exists1       (Set: tSet; Proc: ProcOfCardToBool): BOOLEAN;
  866. PROCEDURE Assign        (VAR Set1: tSet; Set2: tSet);
  867. PROCEDURE AssignElmt    (VAR Set: tSet; Elmt: CARDINAL);
  868. PROCEDURE AssignEmpty   (VAR Set: tSet);
  869. PROCEDURE ForallDo      (Set: tSet; Proc: ProcOfCard);
  870. PROCEDURE ReadSet       (f: tFile; VAR Set: tSet);
  871. PROCEDURE WriteSet      (f: tFile;     Set: tSet);
  872.  
  873. END Sets.
  874.  
  875.  
  876.      Two parameters of type 'tSet' passed to one of the above procedures  must
  877. have  the  same  size, that is they must have been created by passing the same
  878. value 'MaxSize' to the procedure 'MakeSet'. A parameter representing  an  ele-
  879. ment  (of  type  CARDINAL or equivalent) passed to one of the above procedures
  880. must have a value between 0 and 'MaxSize' of the involved  set  which  is  the
  881. other  parameter passed. If the two conditions above, which can be verified at
  882. programming time, don't hold then strange things will  happen,  because  there
  883. are no checks at run time, of course.
  884.  
  885.  
  886.  
  887.  
  888.  
  889.  
  890.  
  891.  
  892.  
  893.  
  894.  
  895.  
  896.  
  897.  
  898.                                     Reuse                                   13
  899.  
  900.  
  901.  
  902. The following table explains the semantics of the set operations:
  903.  
  904. Procedure        Semantics
  905. ___________________________________________________________________________________________
  906. MakeSet          allocates space for a set to hold elements
  907.                  ranging from 0 to 'MaxSize'.
  908. ReleaseSet       releases the space taken by a set.
  909. Union            Set1 := Set1 U Set2
  910. Difference       Set1 := Set1 - Set2
  911. Intersection     Set1 := Set1  Set2
  912. SymDiff          Set1 := Set1  Set2     (* corresponds to exclusive or *)
  913. Complement       Set := { 0 .. MaxSize } - Set
  914. Include          Set := Set U { Elmt }
  915. Exclude          Set := Set - { Elmt }
  916. Card             returns number of elements in Set
  917. Size             returns 'MaxSize' given at creation time
  918. Minimum          returns smallest element x from Set
  919. Maximum          returns largest element x from Set
  920. Select           returns arbitrary element x from Set
  921. Extract          returns arbitrary element x from Set and removes it from Set
  922. IsSubset         Set1  Set2
  923. IsStrictSubset   Set1  Set2
  924. IsEqual          Set1 = Set2
  925. IsNotEqual       Set1 / Set2
  926. IsElement        Elmt  Set
  927. IsEmpty          Set =
  928. Forall            e  Set : Proc (e)   (* predicate Proc must hold for all elements *)
  929. Exists            e  Set : Proc (e)   (* predicate Proc must hold for at least 1 element *)
  930. Exists1          | { e  Set : Proc (e) } | = 1
  931. Assign           Set1 := Set2
  932. AssignElmt       Set1 := { Elmt }
  933. AssignEmpty      Set1 :=
  934. ForallDo         FOR e := 0 TO MaxSize DO
  935.                       IF e  Set THEN Proc (e); END;
  936.                  END;
  937. ReadSet          read external representation of a set from file 'tFile'.
  938. WriteSet         write external representation of a set to file 'tFile'.
  939.                  Example output: { 0 5 6 123}
  940.  
  941.  
  942.  
  943. 11.  SetsC: sets for scalar values
  944.  
  945.      This module provides the same procedures as  the  module  Sets  with  the
  946. difference that all possible run time checks are performed.
  947.  
  948. 12.  Relations: binary relations between scalar values
  949.  
  950.      This module  provides  operations  on  binary  relations  between  scalar
  951. values.  The elements of the relations can be pairs of the types INTEGER, CAR-
  952. DINAL, CHAR, or of an enumeration type. Arguments of type CHAR or of  enumera-
  953. tion  types  have  to  be  coerced  to the type CARDINAL with the function ORD
  954.  
  955.  
  956.  
  957.  
  958.  
  959.  
  960.  
  961.  
  962.  
  963.  
  964.                                     Reuse                                   14
  965.  
  966.  
  967. before use as a parameter of the functions below. The size of the relations is
  968. not  restricted.  The  size is a parameter to the procedure MakeRelation which
  969. dynamically allocates space for arbitrary large relations.
  970.  
  971.      The relations are implemented as bit matrices or  to  be  more  exact  as
  972. arrays  of  sets.  Relations  are  viewed  as sets of pairs. Therefore the set
  973. operations as defined above are also applicable for relations. The  additional
  974. operations have the meaning known from theory.
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.  
  995.  
  996.  
  997.  
  998.  
  999.  
  1000.  
  1001.  
  1002.  
  1003.  
  1004.  
  1005.  
  1006.  
  1007.  
  1008.  
  1009.  
  1010.  
  1011.  
  1012.  
  1013.  
  1014.  
  1015.  
  1016.  
  1017.  
  1018.  
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
  1029.                                     Reuse                                   15
  1030.  
  1031.  
  1032. DEFINITION MODULE Relations;
  1033.  
  1034. TYPE tRelation;
  1035. TYPE ProcOfIntInt       = PROCEDURE (INTEGER, INTEGER);
  1036. TYPE ProcOfIntIntToBool = PROCEDURE (INTEGER, INTEGER): BOOLEAN;
  1037.  
  1038. PROCEDURE MakeRelation  (VAR Rel: tRelation; Size1, Size2: INTEGER);
  1039. PROCEDURE ReleaseRelation (VAR Rel: tRelation);
  1040. PROCEDURE Include       (VAR Rel: tRelation; e1, e2: INTEGER);
  1041. PROCEDURE Exclude       (VAR Rel: tRelation; e1, e2: INTEGER);
  1042. PROCEDURE IsElement     (e1, e2: INTEGER; Rel: tRelation): BOOLEAN;
  1043. PROCEDURE IsRelated     (e1, e2: INTEGER; Rel: tRelation): BOOLEAN;
  1044. PROCEDURE IsReflexive1  (e1: INTEGER; Rel: tRelation): BOOLEAN;
  1045. PROCEDURE IsSymmetric1  (e1, e2: INTEGER; Rel: tRelation): BOOLEAN;
  1046. PROCEDURE IsTransitive1 (e1, e2, e3: INTEGER; Rel: tRelation): BOOLEAN;
  1047. PROCEDURE IsReflexive   (Rel: tRelation): BOOLEAN;
  1048. PROCEDURE IsSymmetric   (Rel: tRelation): BOOLEAN;
  1049. PROCEDURE IsTransitive  (Rel: tRelation): BOOLEAN;
  1050. PROCEDURE IsEquivalence (Rel: tRelation): BOOLEAN;
  1051. PROCEDURE HasReflexive  (Rel: tRelation): BOOLEAN;
  1052. PROCEDURE IsCyclic      (Rel: tRelation): BOOLEAN;
  1053. PROCEDURE Closure       (VAR Rel: tRelation);
  1054. PROCEDURE AssignEmpty   (VAR Rel: tRelation);
  1055. PROCEDURE AssignElmt    (VAR Rel: tRelation; e1, e2: INTEGER);
  1056. PROCEDURE Assign        (VAR Rel1: tRelation; Rel2: tRelation);
  1057. PROCEDURE Union         (VAR Rel1: tRelation; Rel2: tRelation);
  1058. PROCEDURE Difference    (VAR Rel1: tRelation; Rel2: tRelation);
  1059. PROCEDURE Intersection  (VAR Rel1: tRelation; Rel2: tRelation);
  1060. PROCEDURE SymDiff       (VAR Rel1: tRelation; Rel2: tRelation);
  1061. PROCEDURE Complement    (VAR Rel: tRelation);
  1062. PROCEDURE IsSubset      (Rel1, Rel2: tRelation): BOOLEAN;
  1063. PROCEDURE IsStrictSubset (Rel1, Rel2: tRelation): BOOLEAN;
  1064. PROCEDURE IsEqual       (VAR Rel1, Rel2: tRelation): BOOLEAN;
  1065. PROCEDURE IsNotEqual    (Rel1, Rel2: tRelation): BOOLEAN;
  1066. PROCEDURE IsEmpty       (Rel: tRelation): BOOLEAN;
  1067. PROCEDURE Card          (VAR Rel: tRelation): INTEGER;
  1068. PROCEDURE Select        (VAR Rel: tRelation; VAR e1, e2: INTEGER);
  1069. PROCEDURE Extract       (VAR Rel: tRelation; VAR e1, e2: INTEGER);
  1070. PROCEDURE Forall        (Rel: tRelation; Proc: ProcOfIntIntToBool): BOOLEAN;
  1071. PROCEDURE Exists        (Rel: tRelation; Proc: ProcOfIntIntToBool): BOOLEAN;
  1072. PROCEDURE Exists1       (Rel: tRelation; Proc: ProcOfIntIntToBool): BOOLEAN;
  1073. PROCEDURE ForallDo      (Rel: tRelation; Proc: ProcOfIntInt);
  1074. PROCEDURE ReadRelation  (f: tFile; VAR Rel: tRelation);
  1075. PROCEDURE WriteRelation (f: tFile;     Rel: tRelation);
  1076.  
  1077. END Relations.
  1078.  
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093.  
  1094.                                     Reuse                                   16
  1095.  
  1096.  
  1097. 13.  IO: buffered input and output
  1098.  
  1099.      This module provides procedures for buffered input and output  to  files.
  1100. Io  to  and  from terminals is possible too, because terminals are regarded as
  1101. special cases of files. There are procedures for binary io as well as for text
  1102. io, that is internal values are converted to their external representation and
  1103. vice versa.
  1104.  
  1105.  
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.  
  1127.  
  1128.  
  1129.  
  1130.  
  1131.  
  1132.  
  1133.  
  1134.  
  1135.  
  1136.  
  1137.  
  1138.  
  1139.  
  1140.  
  1141.  
  1142.  
  1143.  
  1144.  
  1145.  
  1146.  
  1147.  
  1148.  
  1149.  
  1150.  
  1151.  
  1152.  
  1153.  
  1154.  
  1155.  
  1156.  
  1157.  
  1158.  
  1159.                                     Reuse                                   17
  1160.  
  1161.  
  1162. DEFINITION MODULE IO;
  1163.  
  1164. CONST   StdInput        ;   (* A pre-opened file for (terminal-)input.  *)
  1165. CONST   StdOutput       ;   (* A pre-opened file for (terminal-)output. *)
  1166. CONST   StdError        ;   (* A pre-opened file for (terminal-)output  *)
  1167.                             (*    of error messages.                    *)
  1168. TYPE    tFile           ;
  1169.  
  1170. PROCEDURE ReadOpen      (FileName: ARRAY OF CHAR): tFile;
  1171.                                                 (* open  input file     *)
  1172. PROCEDURE ReadClose     (f: tFile);             (* close input file     *)
  1173. PROCEDURE Read          (f: tFile; Buffer: ADDRESS; Size: CARDINAL): INTEGER;
  1174.                                                 (* binary               *)
  1175. PROCEDURE ReadC         (f: tFile): CHAR    ;   (* character            *)
  1176. PROCEDURE ReadI         (f: tFile): INTEGER ;   (* integer  number      *)
  1177. PROCEDURE ReadR         (f: tFile): REAL    ;   (* real     number      *)
  1178. PROCEDURE ReadB         (f: tFile): BOOLEAN ;   (* boolean              *)
  1179. PROCEDURE ReadN         (f: tFile; Base: INTEGER): INTEGER;
  1180.                                                 (* number of base 'Base'*)
  1181. PROCEDURE ReadS         (f: tFile; VAR s: ARRAY OF CHAR);
  1182.                                                 (* string               *)
  1183. PROCEDURE ReadNl        (f: tFile);             (* new line             *)
  1184. PROCEDURE UnRead        (f: tFile);             (* backspace 1 char.    *)
  1185.  
  1186. PROCEDURE EndOfLine     (f: tFile): BOOLEAN ;   (* end of line ?        *)
  1187. PROCEDURE EndOfFile     (f: tFile): BOOLEAN ;   (* end of file ?        *)
  1188.  
  1189. PROCEDURE WriteOpen     (FileName: ARRAY OF CHAR): tFile;
  1190.                                                 (* open  output file    *)
  1191. PROCEDURE WriteClose    (f: tFile);             (* close output file    *)
  1192. PROCEDURE WriteFlush    (f: tFile);             (* flush output buffer  *)
  1193. PROCEDURE Write         (f: tFile; Buffer: ADDRESS; Size: CARDINAL): INTEGER;
  1194.                                                 (* binary               *)
  1195. PROCEDURE WriteC        (f: tFile; c: CHAR);    (* character            *)
  1196. PROCEDURE WriteI        (f: tFile; n: INTEGER ; FieldWidth: CARDINAL);
  1197.                                                 (* integer  number      *)
  1198. PROCEDURE WriteR        (f: tFile; n: REAL; Before, After, Exp: CARDINAL);
  1199.                                                 (* real     number      *)
  1200. PROCEDURE WriteB        (f: tFile; b: BOOLEAN); (* boolean              *)
  1201. PROCEDURE WriteN        (f: tFile; n: LONGCARD; FieldWidth, Base: CARDINAL);
  1202.                                                 (* number of base 'Base'*)
  1203. PROCEDURE WriteS        (f: tFile; s: ARRAY OF CHAR);
  1204.                                                 (* string               *)
  1205. PROCEDURE WriteNl       (f: tFile);             (* new line             *)
  1206.  
  1207. PROCEDURE CloseIO;                              (* close all files      *)
  1208. END IO.
  1209.  
  1210.  
  1211.  
  1212.  
  1213.  
  1214.  
  1215.  
  1216.  
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222.  
  1223.  
  1224.                                     Reuse                                   18
  1225.  
  1226.  
  1227. Notes:
  1228.  
  1229. ReadOpen and WriteOpen
  1230.      Open the file whose name is given by the parameter 'FileName'  for  input
  1231.      resp. output.  A file descriptor will be returned.
  1232.  
  1233. Read and Write
  1234.      Implement binary i/o for byte blocks.
  1235.  
  1236. ReadI, ReadR, ReadB, ReadN
  1237.      This procedures skip blanks before reading a value.
  1238.  
  1239. ReadR and WriteR
  1240.      'ReadR' reads real numbers according to the following syntax:
  1241.           [+|-] [digit* . digit*] [E [+|-] digit+]
  1242.      'WriteR' writes real numbers similar to the Ada output routine PUT:
  1243.           Before . After E Exp
  1244.      where Before, After, and Exp give the lengths of the fields. If Exp is 0
  1245.      no exponent part is written.
  1246.  
  1247. ReadB and WriteB
  1248.      The external representation for boolean values are T for TRUE  an  F  for
  1249.      FALSE. On input every character other than T is interpreted as FALSE.
  1250.  
  1251. ReadN and WriteN
  1252.      Procedures for i/o of octal (Base = 8) or hexadecimal (Base = 16) numbers
  1253.      for example. Base must have a value between 2 and 16.
  1254.  
  1255. ReadNl
  1256.      Read all characters up to and including the next end of line character.
  1257.  
  1258. EndOfLine
  1259.      Checks whether end of line has been reached. If this is the case the next
  1260.      character to be read would be an end of line character.
  1261.  
  1262. EndOfFile
  1263.      Checks whether end of file has been reached. If this is the case none  of
  1264.      the input routines can yield any defined result.
  1265.  
  1266. WriteFlush
  1267.      The actual contents of the buffer  for  the  specified  file  is  written
  1268.      unconditionally to the file.
  1269.  
  1270. WriteNl
  1271.      Writes an end of line character.
  1272.  
  1273. CloseIO
  1274.      This procedure has to be called at the end of every program that uses the
  1275.      module  IO.  All  buffers  associated  with  files  opened for output are
  1276.      flushed.
  1277.  
  1278.  
  1279.  
  1280.  
  1281.  
  1282.  
  1283.  
  1284.  
  1285.  
  1286.  
  1287.  
  1288.  
  1289.                                     Reuse                                   19
  1290.  
  1291.  
  1292. 14.  StdIO: buffered IO for standard files
  1293.  
  1294.      This module provides the same  procedures  as  the  module  IO  with  the
  1295. difference  that  the parameter 'f' to specify the file is missing.  The input
  1296. (output) procedures use the file 'StdInput' ('StdOutput').
  1297.  
  1298.  
  1299.  
  1300.  
  1301.  
  1302.  
  1303.  
  1304.  
  1305.  
  1306.  
  1307.  
  1308.  
  1309.  
  1310.  
  1311.  
  1312.  
  1313.  
  1314.  
  1315.  
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.  
  1325.  
  1326.  
  1327.  
  1328.  
  1329.  
  1330.  
  1331.  
  1332.  
  1333.  
  1334.  
  1335.  
  1336.  
  1337.  
  1338.  
  1339.  
  1340.  
  1341.  
  1342.  
  1343.  
  1344.  
  1345.  
  1346.  
  1347.  
  1348.  
  1349.  
  1350.  
  1351.  
  1352.  
  1353.  
  1354.                                     Reuse                                   20
  1355.  
  1356.  
  1357. 15.  Layout: more routines for input and output
  1358.  
  1359.  
  1360. DEFINITION MODULE Layout;
  1361.  
  1362. PROCEDURE WriteChar     (f: tFile; Ch: CHAR);
  1363.                         (* Printable characters are surrounded by       *)
  1364.                         (* quotes. Non-printable characters are written *)
  1365.                         (* as their internal code followed by a 'C'.    *)
  1366.  
  1367. PROCEDURE WriteSpace    (f: tFile);
  1368.                         (* Write a blank character to file 'f'.         *)
  1369.  
  1370. PROCEDURE WriteSpaces   (f: tFile; Count: INTEGER);
  1371.                         (* Write 'Count' blank characters to file 'f'.  *)
  1372.  
  1373. PROCEDURE ReadSpace     (f: tFile);
  1374.                         (* Skip one character of file 'f'.              *)
  1375.  
  1376. PROCEDURE ReadSpaces    (f: tFile; Count: INTEGER);
  1377.                         (* Skip 'Count' characters of file 'f'.         *)
  1378.  
  1379. PROCEDURE SkipSpaces    (f: tFile);
  1380.                         (* Skip as many blank characters as possible.   *)
  1381.  
  1382. END Layout.
  1383.  
  1384.  
  1385. 16.  Positions: handling of source positions
  1386.  
  1387.      A simple representation of the position of tokens in a source  file  con-
  1388. sisting  of  a  line  and  a  column  field.  This module should be copied and
  1389. tailored to the user's needs, if necessary. Modifications may be necessary  if
  1390. the  type SHORTCARD is to small to count the lines or an extra field is needed
  1391. to describe the source file.
  1392.  
  1393.  
  1394.  
  1395.  
  1396.  
  1397.  
  1398.  
  1399.  
  1400.  
  1401.  
  1402.  
  1403.  
  1404.  
  1405.  
  1406.  
  1407.  
  1408.  
  1409.  
  1410.  
  1411.  
  1412.  
  1413.  
  1414.  
  1415.  
  1416.  
  1417.  
  1418.  
  1419.                                     Reuse                                   21
  1420.  
  1421.  
  1422. DEFINITION MODULE Positions;
  1423.  
  1424. TYPE      tPosition     = RECORD Line, Column: SHORTCARD; END;
  1425.  
  1426. VAR       NoPosition    : tPosition;
  1427.                         (* A default position (0, 0).                   *)
  1428.  
  1429. PROCEDURE Compare       (Position1, Position2: tPosition): INTEGER;
  1430.                         (* Returns -1 if Position1 < Position2.         *)
  1431.                         (* Returns  0 if Position1 = Position2.         *)
  1432.                         (* Returns  1 if Position1 > Position2.         *)
  1433.  
  1434. PROCEDURE WritePosition (File: tFile; Position: tPosition);
  1435.                         (* The 'Position' is printed on the 'File'.     *)
  1436.  
  1437. END Positions.
  1438.  
  1439.  
  1440.  
  1441.  
  1442.  
  1443.  
  1444.  
  1445.  
  1446.  
  1447.  
  1448.  
  1449.  
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.  
  1457.  
  1458.  
  1459.  
  1460.  
  1461.  
  1462.  
  1463.  
  1464.  
  1465.  
  1466.  
  1467.  
  1468.  
  1469.  
  1470.  
  1471.  
  1472.  
  1473.  
  1474.  
  1475.  
  1476.  
  1477.  
  1478.  
  1479.  
  1480.  
  1481.  
  1482.  
  1483.  
  1484.                                     Reuse                                   22
  1485.  
  1486.  
  1487. 17.  Errors: error handler for parsers and compilers
  1488.  
  1489.      This module is needed by parsers generated  with  the  parser  generators
  1490. lalr  or  ell.  It can also b used to report error messages found during scan-
  1491. ning or semantic analysis.  Note: This module has to be copied,  too,  if  the
  1492. module Positions is copied and modified because it depends upon this module.
  1493.  
  1494.  
  1495. DEFINITION MODULE Errors;
  1496.  
  1497. CONST
  1498.    NoText               = 0     ;
  1499.    SyntaxError          = 1     ;       (* error codes          *)
  1500.    ExpectedTokens       = 2     ;
  1501.    RestartPoint         = 3     ;
  1502.    TokenInserted        = 4     ;
  1503.    WrongParseTable      = 5     ;
  1504.    OpenParseTable       = 6     ;
  1505.    ReadParseTable       = 7     ;
  1506.    TooManyErrors        = 8     ;
  1507.  
  1508.    Fatal                = 1     ;       (* error classes        *)
  1509.    Restriction          = 2     ;
  1510.    Error                = 3     ;
  1511.    Warning              = 4     ;
  1512.    Repair               = 5     ;
  1513.    Note                 = 6     ;
  1514.    Information          = 7     ;
  1515.  
  1516.    None                 = 0     ;
  1517.    Integer              = 1     ;       (* info classes         *)
  1518.    Short                = 2     ;
  1519.    Long                 = 3     ;
  1520.    Real                 = 4     ;
  1521.    Boolean              = 5     ;
  1522.    Character            = 6     ;
  1523.    String               = 7     ;
  1524.    Array                = 8     ;
  1525.    Set                  = 9     ;
  1526.    Ident                = 10    ;
  1527.  
  1528.  
  1529.  
  1530.  
  1531.  
  1532.  
  1533.  
  1534.  
  1535.  
  1536.  
  1537.  
  1538.  
  1539.  
  1540.  
  1541.  
  1542.  
  1543.  
  1544.  
  1545.  
  1546.  
  1547.  
  1548.  
  1549.                                     Reuse                                   23
  1550.  
  1551.  
  1552. VAR       Exit          : PROC;
  1553.                         (* Refers to a procedure that specifies         *)
  1554.                         (* what to do if 'ErrorClass' = Fatal.          *)
  1555.                         (* Default: terminate program execution.        *)
  1556.  
  1557. PROCEDURE StoreMessages (Store: BOOLEAN);
  1558.                         (* Messages are stored if 'Store' = TRUE        *)
  1559.                         (* for printing with the routine 'WriteMessages'*)
  1560.                         (* otherwise they are printed immediately.      *)
  1561.                         (* If 'Store'=TRUE the message store is cleared.*)
  1562.  
  1563. PROCEDURE ErrorMessage  (ErrorCode, ErrorClass: CARDINAL; Position: tPosition);
  1564.                         (* Report a message represented by an integer   *)
  1565.                         (* 'ErrorCode' and classified by 'ErrorClass'.  *)
  1566.  
  1567. PROCEDURE ErrorMessageI (ErrorCode, ErrorClass: CARDINAL; Position: tPosition;
  1568.                          InfoClass: CARDINAL; Info: ADDRESS);
  1569.                         (* Like the previous routine with additional    *)
  1570.                         (* information of type 'InfoClass' at the       *)
  1571.                         (* address 'Info'.                              *)
  1572.  
  1573. PROCEDURE Message       (ErrorText: ARRAY OF CHAR; ErrorClass: CARDINAL;
  1574.                          Position: tPosition);
  1575.                         (* Report a message represented by a string     *)
  1576.                         (* 'ErrorText' and classified by 'ErrorClass'.  *)
  1577.  
  1578. PROCEDURE MessageI      (ErrorText: ARRAY OF CHAR; ErrorClass: CARDINAL;
  1579.                          Position: tPosition; InfoClass: CARDINAL; Info: ADDRESS);
  1580.                         (* Like the previous routine with additional    *)
  1581.                         (* information of type 'InfoClass' at the       *)
  1582.                         (* address 'Info'.                              *)
  1583.  
  1584. PROCEDURE WriteMessages (File: tFile);
  1585.                         (* The stored messages are sorted by their      *)
  1586.                         (* source position and printed on 'File'.       *)
  1587.  
  1588. END Errors.
  1589.  
  1590.  
  1591.  
  1592.  
  1593.  
  1594.  
  1595.  
  1596.  
  1597.  
  1598.  
  1599.  
  1600.  
  1601.  
  1602.  
  1603.  
  1604.  
  1605.  
  1606.  
  1607.  
  1608.  
  1609.  
  1610.  
  1611.  
  1612.  
  1613.  
  1614.                                     Reuse                                   24
  1615.  
  1616.  
  1617. 18.  Source: provides input for scanners
  1618.  
  1619.      This module is needed by scanners generated with  the  scanner  generator
  1620. rex.
  1621.  
  1622.  
  1623. DEFINITION MODULE Source;
  1624.  
  1625. PROCEDURE BeginSource (FileName: ARRAY OF CHAR): tFile;
  1626.  
  1627.    (*
  1628.       BeginSource is called from the scanner to open files.
  1629.       If not called then input is read form standard input.
  1630.    *)
  1631.  
  1632. PROCEDURE GetLine (File: tFile; Buffer: ADDRESS; Size: CARDINAL): INTEGER;
  1633.  
  1634.    (*
  1635.       GetLine is called to fill a buffer starting at address 'Buffer'
  1636.       with a block of maximal 'Size' characters. Lines are terminated
  1637.       by newline characters (ASCII = 0xa). GetLine returns the number
  1638.       of characters transferred. Reasonable block sizes are between 128
  1639.       and 2048 or the length of a line. Smaller block sizes -
  1640.       especially block size 1 - will drastically slow down the scanner.
  1641.    *)
  1642.  
  1643. PROCEDURE CloseSource (File: tFile);
  1644.  
  1645.    (*
  1646.       CloseSource is called from the scanner at end of file or
  1647.       at end of input, respectively. It can be used to close files.
  1648.    *)
  1649.  
  1650. END Source.
  1651.  
  1652.  
  1653.  
  1654.  
  1655.  
  1656.  
  1657.  
  1658.  
  1659.  
  1660.  
  1661.  
  1662.  
  1663.  
  1664.  
  1665.  
  1666.  
  1667.  
  1668.  
  1669.  
  1670.  
  1671.  
  1672.  
  1673.  
  1674.  
  1675.  
  1676.  
  1677.  
  1678.  
  1679.                                     Reuse                                   25
  1680.  
  1681.  
  1682. 19.  Sort: quicksort for arrays with elements of arbitrary type
  1683.  
  1684.  
  1685. DEFINITION MODULE Sort;
  1686.  
  1687. TYPE tProcIntIntBool    = PROCEDURE (INTEGER, INTEGER): BOOLEAN;
  1688. TYPE tProcIntInt        = PROCEDURE (INTEGER, INTEGER);
  1689.  
  1690. PROCEDURE Sort (Lwb, Upb: INTEGER; IsLess: tProcIntIntBool; Swap: tProcIntInt);
  1691.  
  1692.         (* Sort data from the indices 'Lwb' to 'Upb' using quicksort.   *)
  1693.         (* The procedures 'IsLess' and 'Swap' are used to compare and   *)
  1694.         (* exchange two data elements.                                  *)
  1695.  
  1696. END Sort.
  1697.  
  1698.  
  1699. 20.  General: miscellaneous functions
  1700.  
  1701.  
  1702. DEFINITION MODULE General;
  1703.  
  1704. PROCEDURE Min           (a, b: INTEGER)                 : INTEGER;
  1705.                         (* Returns the minimum of 'a' and 'b'.          *)
  1706.  
  1707. PROCEDURE Max           (a, b: INTEGER)                 : INTEGER;
  1708.                         (* Returns the maximum of 'a' and 'b'.          *)
  1709.  
  1710. PROCEDURE Log2          (x: LONGINT)                    : CARDINAL;
  1711.                         (* Returns the logarithm to the base 2 of 'x'.  *)
  1712.  
  1713. PROCEDURE Exp2          (x: CARDINAL)                   : LONGINT;
  1714.                         (* Returns 2 to the power of 'x'.               *)
  1715.  
  1716. PROCEDURE AntiLog       (x: LONGINT)                    : CARDINAL;
  1717.                         (* Returns the number of the lowest bit set in 'x'. *)
  1718.  
  1719. PROCEDURE Exp10         (x: INTEGER)                    : REAL;
  1720.                         (* Returns 10 to the power of 'x'.              *)
  1721.  
  1722. END General.
  1723.  
  1724.  
  1725.  
  1726.  
  1727.  
  1728.  
  1729.  
  1730.  
  1731.  
  1732.  
  1733.  
  1734.  
  1735.  
  1736.  
  1737.  
  1738.  
  1739.  
  1740.  
  1741.  
  1742.  
  1743.  
  1744.                                     Reuse                                   26
  1745.  
  1746.  
  1747. 21.  System: machine dependent code
  1748.  
  1749.      This module provides a few machine dependent operations.
  1750.  
  1751.  
  1752. FOREIGN MODULE System;
  1753.  
  1754. CONST   cMaxFile    = 32;
  1755. CONST   StdInput        ;   (* A pre-opened file for (terminal-)input.  *)
  1756. CONST   StdOutput       ;   (* A pre-opened file for (terminal-)output. *)
  1757. CONST   StdError        ;   (* A pre-opened file for (terminal-)output  *)
  1758.                             (*    of error messages.                    *)
  1759.  
  1760. TYPE    tFile           ;
  1761.  
  1762.                         (* binary IO            *)
  1763.  
  1764. PROCEDURE OpenInput     (VAR FileName: ARRAY OF CHAR): tFile;
  1765.                         (* Opens the file whose name is given by the    *)
  1766.                         (* parameter 'FileName' for input.              *)
  1767.                         (* Returns a file descriptor.                   *)
  1768.  
  1769. PROCEDURE OpenOutput    (VAR FileName: ARRAY OF CHAR): tFile;
  1770.                         (* Opens the file whose name is given by the    *)
  1771.                         (* parameter 'FileName' for output.             *)
  1772.                         (* Returns a file descriptor.                   *)
  1773.  
  1774. PROCEDURE Read          (File: tFile; Buffer: ADDRESS; Size: CARDINAL): INTEGER;
  1775.                         (* Reads 'Size' bytes from file 'tFile' and     *)
  1776.                         (* stores them in a buffer starting at address  *)
  1777.                         (* 'Buffer'.                                    *)
  1778.                         (* Returns the number of bytes actually read.   *)
  1779.  
  1780. PROCEDURE Write         (File: tFile; Buffer: ADDRESS; Size: CARDINAL): INTEGER;
  1781.                         (* Writes 'Size' bytes from a buffer starting   *)
  1782.                         (* at address 'Buffer' to file 'tFile'.         *)
  1783.                         (* Returns the number of bytes actually written.*)
  1784.  
  1785. PROCEDURE Close         (File: tFile);
  1786.                         (* Closes file 'tFile'.                         *)
  1787.  
  1788. PROCEDURE IsCharacterSpecial (File: tFile): BOOLEAN;
  1789.                         (* Returns TRUE when file 'tFile' is connected  *)
  1790.                         (* to a character device like a terminal.       *)
  1791.  
  1792.                         (* calls other than IO  *)
  1793.  
  1794. PROCEDURE SysAlloc      (ByteCount: LONGINT): ADDRESS;
  1795.                         (* Returns a pointer to dynamically allocated   *)
  1796.                         (* memory space of size 'ByteCount' bytes.      *)
  1797.                         (* Returns NIL if space is exhausted.           *)
  1798.  
  1799. PROCEDURE Time          (): LONGINT;
  1800.  
  1801.  
  1802.  
  1803.  
  1804.  
  1805.  
  1806.  
  1807.  
  1808.  
  1809.  
  1810.                                     Reuse                                   27
  1811.  
  1812.  
  1813.                         (* Returns consumed cpu-time in milliseconds.   *)
  1814.  
  1815. PROCEDURE GetArgCount   (): CARDINAL;
  1816.                         (* Returns number of arguments.                 *)
  1817.  
  1818. PROCEDURE GetArgument   (ArgNum: CARDINAL; VAR Argument: ARRAY OF CHAR);
  1819.                         (* Stores a string-valued argument whose index  *)
  1820.                         (* is 'ArgNum' in the memory area 'Argument'.   *)
  1821.  
  1822. PROCEDURE PutArgs       (Argc: CARDINAL; Argv: ADDRESS);
  1823.                         (* Dummy procedure that passes the values       *)
  1824.                         (* 'argc' and 'argv' from Modula-2 to C.        *)
  1825.  
  1826. PROCEDURE ErrNum        (): INTEGER;
  1827.                         (* Returns the current system error code.       *)
  1828.  
  1829. PROCEDURE System        (VAR String: ARRAY OF CHAR): INTEGER;
  1830.                         (* Executes an operating system command given   *)
  1831.                         (* as the string 'String'. Returns an exit or   *)
  1832.                         (* return code.                                 *)
  1833.  
  1834. PROCEDURE Exit          (Status: INTEGER);
  1835.                         (* Terminates program execution and passes the  *)
  1836.                         (* value 'Status' to the operating system.      *)
  1837.  
  1838. END System.
  1839.  
  1840.  
  1841. 22.  Checks: checks for system calls
  1842.  
  1843.  
  1844. DEFINITION MODULE Checks;
  1845.  
  1846. PROCEDURE ErrorCheck    (s: ARRAY OF CHAR; n: INTEGER);
  1847.                         (* The parameter 'n' should be a value returned *)
  1848.                         (* from a system call. If it is negative the    *)
  1849.                         (* string 's', the value of 'n', and the value  *)
  1850.                         (* returned by 'ErrNum' are printed on the file *)
  1851.                         (* 'StdError'.                                  *)
  1852.  
  1853. END Checks.
  1854.  
  1855.  
  1856. Example:
  1857.  
  1858.  
  1859.    VAR n: INTEGER;
  1860.  
  1861.    n := OpenInput ("DataFile");
  1862.    ErrorCheck ("error at open of DataFile", n);
  1863.  
  1864.  
  1865.  
  1866.  
  1867.  
  1868.  
  1869.  
  1870.  
  1871.  
  1872.  
  1873.  
  1874.  
  1875.                                     Reuse                                   28
  1876.  
  1877.  
  1878. 23.  Times: access to cpu-time
  1879.  
  1880.  
  1881. DEFINITION MODULE Times;
  1882.  
  1883. PROCEDURE CpuTime       (): LONGINT;
  1884.                         (* Returns the sum of user time and system time *)
  1885.                         (* since the start of the program in milli-secs.*)
  1886.  
  1887. PROCEDURE StepTime      (): LONGINT;
  1888.                         (* Returns the sum of user time and system time *)
  1889.                         (* since the last call to 'StepTime' in milli-  *)
  1890.                         (* seconds. The first call yields the same      *)
  1891.                         (* result as a call to 'CpuTime'.               *)
  1892.  
  1893. PROCEDURE WriteStepTime (Text: ARRAY OF CHAR);
  1894.                         (* Writes a line consisting of the string       *)
  1895.                         (* 'Text' and the value obtained from a call to *)
  1896.                         (* 'StepTime' to the file 'StdOutput'.          *)
  1897.  
  1898. END Times.
  1899.  
  1900.  
  1901. 24.  An Example: Text represented as a List of Strings
  1902.  
  1903.  
  1904.  
  1905.  
  1906.  
  1907.  
  1908.  
  1909.  
  1910.  
  1911.  
  1912.  
  1913.  
  1914.  
  1915.  
  1916.  
  1917.  
  1918.  
  1919.  
  1920.  
  1921.  
  1922.  
  1923.  
  1924.  
  1925.  
  1926.  
  1927.  
  1928.  
  1929.  
  1930.  
  1931.  
  1932.  
  1933.  
  1934.  
  1935.  
  1936.  
  1937.  
  1938.  
  1939.  
  1940.                                     Reuse                                   29
  1941.  
  1942.  
  1943. MODULE Example;
  1944.  
  1945. FROM SYSTEM       IMPORT ADDRESS;
  1946. FROM IO           IMPORT StdInput, StdOutput, EndOfFile;
  1947. FROM Strings      IMPORT tString, ReadL, WriteL;
  1948. FROM StringMem    IMPORT tStringRef, PutString, GetString;
  1949. FROM Lists        IMPORT tList, MakeList, Append, IsEmpty, Head, Tail;
  1950.  
  1951. VAR String      : tString;
  1952. VAR Ref         : tStringRef;
  1953. VAR Text        : tList;
  1954.  
  1955. BEGIN
  1956.    MakeList (Text);                     (* start with empty list        *)
  1957.    WHILE NOT EndOfFile (StdInput) DO    (* process complete input file  *)
  1958.       ReadL (StdInput, String);         (* read one line                *)
  1959.       Ref := PutString (String);        (* store it in string memory    *)
  1960.       Append (Text, ADDRESS (Ref));     (* append it to current text    *)
  1961.    END;
  1962.  
  1963.    WHILE NOT IsEmpty (Text) DO          (* process complete text        *)
  1964.       Ref := tStringRef (Head (Text));  (* get current first line       *)
  1965.       GetString (Ref, String);          (* fetch string from memory     *)
  1966.       WriteL (StdOutput, String);       (* print string in one line     *)
  1967.       Tail (Text);                      (* continue with remaining text *)
  1968.    END;
  1969. END Example.
  1970.  
  1971.  
  1972.  
  1973.  
  1974.  
  1975.  
  1976.  
  1977.  
  1978.  
  1979.  
  1980.  
  1981.  
  1982.  
  1983.  
  1984.  
  1985.  
  1986.  
  1987.  
  1988.  
  1989.  
  1990.  
  1991.  
  1992.  
  1993.  
  1994.  
  1995.  
  1996.  
  1997.  
  1998.  
  1999.  
  2000.  
  2001.  
  2002.  
  2003.  
  2004.  
  2005.                                     Reuse                                    1
  2006.  
  2007.  
  2008. Contents
  2009.  
  2010.         Abstract ........................................................    2
  2011. 1.      Overview ........................................................    2
  2012. 2.      Memory: dynamic storage (heap) with free lists ..................    2
  2013. 3.      Heap: dynamic storage (heap) without free lists .................    4
  2014. 4.      DynArray: dynamic and flexible arrays ...........................    4
  2015. 5.      Strings: string handling ........................................    6
  2016. 6.      StringMem: string memory ........................................    8
  2017. 7.      Idents: identifier table - unambiguous encoding of strings ......    9
  2018. 8.      Lists: lists of arbitrary objects ...............................   10
  2019. 9.      Texts: texts are lists of strings (lines) .......................   11
  2020. 10.     Sets: sets for scalar values ....................................   11
  2021. 11.     SetsC: sets for scalar values ...................................   13
  2022. 12.     Relations: binary relations between scalar values ...............   13
  2023. 13.     IO: buffered input and output ...................................   16
  2024. 14.     StdIO: buffered IO for standard files ...........................   19
  2025. 15.     Layout: more routines for input and output ......................   20
  2026. 16.     Positions: handling of source positions .........................   20
  2027. 17.     Errors: error handler for parsers and compilers .................   22
  2028. 18.     Source: provides input for scanners .............................   24
  2029. 19.     Sort: quicksort for arrays with elements of arbitrary type ......   25
  2030. 20.     General: miscellaneous functions ................................   25
  2031. 21.     System: machine dependent code ..................................   26
  2032. 22.     Checks: checks for system calls .................................   27
  2033. 23.     Times: access to cpu-time .......................................   28
  2034. 24.     An Example: Text represented as a List of Strings ...............   28
  2035.  
  2036.  
  2037.  
  2038.  
  2039.  
  2040.  
  2041.  
  2042.  
  2043.  
  2044.  
  2045.  
  2046.  
  2047.  
  2048.  
  2049.  
  2050.  
  2051.  
  2052.  
  2053.  
  2054.  
  2055.  
  2056.  
  2057.  
  2058.  
  2059.  
  2060.  
  2061.  
  2062.  
  2063.  
  2064.  
  2065.  
  2066.